﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_Seguimiento_POA_Especifico
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'llenado de datos iniciales
        If (Not (Page.IsPostBack)) Then
            ViewState("Persona") = ""
            Dim dtUsuarios As System.Data.DataTable = New System.Data.DataTable
            ListaPPER()
            ListaRegion()
            ListaEjecutor("", "", "")
            CargarSemaforo()
            ListaOrden()
            PeriodoUltimo()
            CargarGrilla("", "", "", "", ddlSemaforo.SelectedValue.ToString, "")
        End If
    End Sub

    Public Sub ListaPPER()
        'carga de combo pper
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("PPER") = ""
        objData.Rows.Add(drusuariorow)
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "DESCRIPCION"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
        ddlPPER.SelectedValue = ""
    End Sub

    Public Sub CargarGrilla(ByVal strPPER As String, ByVal strNombre As String, ByVal strRegion As String, ByVal strEjecutor As String, ByVal strSemaforo As String, ByVal strOrden As String)
        'carga la grilla segun filtro
        Dim objPOABC As ReporteBC
        Dim objData As DataTable
        objPOABC = New ReporteBC

        If (Session("perfil") <> "AD") And (Session("perfil") <> "DI") Then
            ViewState("Persona") = Session("persona")
        End If
        objData = objPOABC.ListaPOA_Reporte_Especifico(strPPER, strNombre, strRegion, strEjecutor, strSemaforo, "", ViewState("Periodo"), strOrden)
        If Session("AFDetalle") = 1 Then
            If Session("Pagina") IsNot Nothing Then
                gv_impresion.PageIndex = Session("Pagina")
            End If
            Session("AFDetalle") = 0
        End If
        gv_impresion.DataSource = objData
        gv_impresion.DataBind()

        gv_doc.DataSource = objData
        gv_doc.DataBind()
    End Sub

    Public Sub ListaRegion()
        'cargamos combo region
        Dim objGeneral As GeneralBC
        Dim objData As DataTable
        objGeneral = New GeneralBC
        objData = New DataTable
        objData = objGeneral.ListaRegion("")
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("NOMBRE") = "--Todos--"
        drusuariorow("UBIGEO") = ""
        objData.Rows.Add(drusuariorow)
        ddlRegion.DataSource = objData
        ddlRegion.DataTextField = "NOMBRE"
        ddlRegion.DataValueField = "UBIGEO"
        ddlRegion.DataBind()
        ddlRegion.SelectedValue = ""
    End Sub

    Public Sub ListaEjecutor(ByVal strPPER As String, ByVal strRegion As String, ByVal strAmbito As String)
        'cargamos combo de ejecutor
        Dim objReporte As ReporteBC
        Dim objData As DataTable
        objReporte = New ReporteBC
        objData = New DataTable
        objData = objReporte.ListaEjecutor(strPPER, strRegion, strAmbito)
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("EJECUTOR") = ""
        objData.Rows.Add(drusuariorow)
        ddlEjecutor.DataSource = objData
        ddlEjecutor.DataTextField = "DESCRIPCION"
        ddlEjecutor.DataValueField = "EJECUTOR"
        ddlEjecutor.DataBind()
        ddlEjecutor.SelectedValue = ""
    End Sub

    Public Sub CargarSemaforo()
        'cargamos combo semaforo
        Dim objGeneral As GeneralBC
        Dim objDt As DataTable
        Try
            objGeneral = New GeneralBC
            objDt = New DataTable
            objDt = objGeneral.ListaTablaGeneral("SEMAFORO")
            Dim drusuariorow As DataRow = objDt.NewRow()
            drusuariorow("DESCRIPCION") = "--Todos--"
            drusuariorow("CODIGO") = ""
            objDt.Rows.Add(drusuariorow)
            ddlSemaforo.DataSource = objDt
            objDt.Rows(3).Delete()
            ddlSemaforo.DataTextField = "DESCRIPCION"
            ddlSemaforo.DataValueField = "CODIGO"
            ddlSemaforo.DataBind()
            ddlSemaforo.SelectedValue = "3"
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub ListaOrden()
        'cargamos combo e orden
        Dim objData As DataTable
        objData = New DataTable
        Dim COL1 As DataColumn
        Dim COL2 As DataColumn

        COL1 = New DataColumn("NOMBRE", System.Type.GetType("System.String"))
        COL2 = New DataColumn("CODIGO", System.Type.GetType("System.String"))

        objData.Columns.Add(COL1)
        objData.Columns.Add(COL2)
        Dim drusuariorow1, drusuariorow2, drusuariorow3 As DataRow
        drusuariorow1 = objData.NewRow()
        drusuariorow1("NOMBRE") = "--Todos--"
        drusuariorow1("CODIGO") = ""
        objData.Rows.Add(drusuariorow1)
        drusuariorow2 = objData.NewRow()
        drusuariorow2("NOMBRE") = "ASCENDENTE"
        drusuariorow2("CODIGO") = "ASC"
        objData.Rows.Add(drusuariorow2)
        drusuariorow3 = objData.NewRow()
        drusuariorow3("NOMBRE") = "DESCENDENTE"
        drusuariorow3("CODIGO") = "DESC"
        objData.Rows.Add(drusuariorow3)
        ddlOrden.DataSource = objData
        ddlOrden.DataTextField = "NOMBRE"
        ddlOrden.DataValueField = "CODIGO"
        ddlOrden.DataBind()
        ddlOrden.SelectedValue = ""
    End Sub

    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        Session("Pagina") = Nothing
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub buscar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles buscar.Click
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub gv_impresion_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_impresion.PageIndexChanging
        'cargamos segun pagina
        Dim newPageNumber As Integer
        If Session("Pagina") IsNot Nothing Then
            newPageNumber = Session("Pagina")
        Else
            newPageNumber = e.NewPageIndex + 1
        End If
        gv_impresion.PageIndex = e.NewPageIndex
        Session("Pagina") = e.NewPageIndex
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
        gv_impresion.SelectedIndex = -1
    End Sub

    Protected Sub gv_impresion_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_impresion.RowDataBound
        'formato de celda 5
        If e.Row.RowIndex >= 0 Then
            Select Case e.Row.Cells(6).Text
                Case 1
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(0, 128, 0)
                Case 2
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(250, 150, 0)
                Case 3
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(250, 50, 50)
                Case 4
                    e.Row.Cells(6).BackColor = Drawing.Color.White
            End Select
            e.Row.Cells(6).Text = ""
        End If

        ' FORMATEA ROWS
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_impresion, "Select$" + e.Row.RowIndex.ToString)
        End If
    End Sub

    Protected Sub gv_impresion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_impresion.SelectedIndexChanged
        Session("Pagina") = gv_impresion.PageIndex
        'capturamos parametros de fila seleccionada
        Dim hfPOA As HiddenField = CType(gv_impresion.SelectedRow.FindControl("POA"), HiddenField)
        Dim strPPER As Label = CType(gv_impresion.SelectedRow.FindControl("lblPPER"), Label)
        Dim strEjecutor As String = gv_impresion.SelectedRow.Cells(3).Text.ToString
        Dim hfPPER As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfPPER"), HiddenField)
        Dim hfEjecutor As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfEjecutor"), HiddenField)
        Dim strNombre As String = gv_impresion.SelectedRow.Cells(5).Text.ToString
        Dim strPeriodo As String = gv_impresion.SelectedRow.Cells(7).Text.ToString
        ViewState("Monitoreador") = Server.HtmlDecode(gv_impresion.SelectedRow.Cells(12).Text.ToString)
        ViewState("Especialista") = Server.HtmlDecode(gv_impresion.SelectedRow.Cells(13).Text.ToString)
        Dim hfRegion As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfRegion"), HiddenField)
        Try
            ViewState("POA") = Server.HtmlDecode(hfPOA.Value.ToString)
            ViewState("Nombre") = Server.HtmlDecode(strNombre)
            ViewState("Ejecutor") = Server.HtmlDecode(hfEjecutor.Value.ToString)
            ViewState("Eje_Des") = Server.HtmlDecode(strEjecutor)
            ViewState("PPER") = Server.HtmlDecode(strPPER.Text)
            ViewState("PPER_ID") = Server.HtmlDecode(hfPPER.Value.ToString)
            ViewState("PERIODO") = Server.HtmlDecode(strPeriodo)
            ViewState("REGION") = Server.HtmlDecode(hfRegion.Value.ToString)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAgregar.Click
        'redireccionamos si el avance aun no ha sido inrgesado
        Dim objReporte As ReporteBC = New ReporteBC
        If gv_impresion.SelectedIndex > -1 Then
            If ViewState("PERIODO").ToString = "Ingresado" Then
                alerta.Mostrar_Alerta("Aviso", "El Avance Semanal ya fué ingresado")
            Else
                Response.Redirect("ReporteSeguimiento_POA_Especifico.aspx?Nombre=" + ViewState("Nombre").ToString + "&PPER=" + ViewState("PPER").ToString + _
                              "&PPER_ID=" + ViewState("PPER_ID").ToString + "&Ejecutor=" + ViewState("Ejecutor").ToString + _
                              "&Eje_Des=" + ViewState("Eje_Des") + "&POA=" + ViewState("POA").ToString + "&Region=" + ViewState("REGION").ToString + _
                              "&Persona=" + ViewState("Persona") + "&Monitoreador=" + ViewState("Monitoreador") + "&Especialista=" + ViewState("Especialista").ToString)
            End If
        Else
        alerta.Mostrar_Alerta("Aviso", "Debe seleccionar un registro")
        End If
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportar.Click
        ExportarGrilla(gv_doc)
    End Sub

    Protected Sub PeriodoUltimo()
        'capturamos el ultimo periodo
        Dim objGeneral As GeneralBC
        Dim objDt As DataTable
        Try
            objGeneral = New GeneralBC
            objDt = New DataTable
            objDt = objGeneral.ListaPeriodoReporte("0", "Desc", Session("Año"))
            If objDt.Rows.Count > 0 Then
                ViewState("Periodo") = objDt.Rows(0).Item("PERIODO").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub ExportarGrilla(ByVal grilla As GridView)
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        'CREAR TITULO
        Dim gvtitle As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.ColumnSpan = 13
        titulo1.Text = "SEGUIMIENTO DE PROYECTOS / ACTIVIDADES"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 12
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        gvtitle.Rows.AddAt(0, headertitulo1)
        '------------
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle)
        form.Controls.Add(grilla)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=SEGUIMIENTO_PROYECTOS_ACTIVIDADES_" + Now.Date.Year.ToString + Now.Date.Month.ToString + Now.Date.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub gv_doc_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_doc.RowDataBound
        'da formato a grilla
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            Select Case e.Row.Cells(6).Text
                Case "1"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(0, 176, 80)
                Case "2"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(255, 192, 0)
                Case "3"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(255, 0, 0)
            End Select
            e.Row.Cells(6).Text = ""
        End If
    End Sub

    Protected Sub ddlPPER_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPPER.SelectedIndexChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub txtNombre_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNombre.TextChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub ddlRegion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegion.SelectedIndexChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub ddlEjecutor_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEjecutor.SelectedIndexChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub ddlSemaforo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSemaforo.SelectedIndexChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

    Protected Sub ddlOrden_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrden.SelectedIndexChanged
        CargarGrilla(ddlPPER.SelectedValue.ToString, txtNombre.Text.ToString, ddlRegion.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, ddlSemaforo.SelectedValue.ToString, ddlOrden.SelectedValue.ToString)
    End Sub

End Class
